<template>
	<view class="container">
		<view class="uni-tab-bar">
			<scroll-view id="tab-bar" class="uni-swiper-tab" scroll-x :scroll-left="scrollLeft">
				<view v-for="(tab,index) in tabBars" :key="tab.id" class="swiper-tab-list" :class="tabIndex==index ? 'active' : ''"
				 :id="tab.id" :data-current="index" @click="tapTab">
					<view>{{tab.name}}</view>
				</view>
			</scroll-view>
			<swiper class="swiper-box" :duration="300" @change="changeTab">
				<!-- 地图 -->
				<swiper-item v-if="tabIndex==0">
					<scroll-view class="list">
						<view class="map">
							<map :latitude="latitude" :longitude="longitude" :markers="covers"></map>
						</view>
						<!-- 轮播广告 -->
						<view style="height:30vh;position: relative;">
							<swiper class="article">									
								<swiper-item class="cover-item" style="background-image: url('/static/share_img/share_img.png');">
									<view>
										<view class="maxWord">邀请会员</view>
										<view class="smallWord">分享海报邀请会员</view>
										<button class="yaoBtn">立即邀请</button>
									</view>			
								</swiper-item>
								<swiper-item class="cover-item" style="background-image: url('/static/share_img/pinpai.png')">					
									<view>
										<view class="maxWord">品牌入驻</view>
										<view class="smallWord">查看更多合作品牌</view>
										<button class="yaoBtn seeBtn">立即查看</button>
									</view>								
								</swiper-item>
							</swiper >
						</view>						
					</scroll-view>
				</swiper-item>
				<!-- 返现记录 -->
				<swiper-item v-if="tabIndex==1">
					<scroll-view class="list" scroll-y @scrolltolower="loadMore">
						<view class="total">
							<view>{{total}}</view>
							<text>全部返现金额</text>
						</view>
						<view class="line"></view>
						<view class="box border_bottom_ef" v-for="(item, index) in billList" :key="index">
							<view class="left">
								<text class="font30">{{item.title}}</text>
								<text class="date color_title m_t6">{{item.date}}</text>
							</view>
							<view class="right font36">
								{{item.money}}
							</view>
						</view>
						<!-- <view class="uni-tab-bar-loading">
							{{loadingText}}
						</view> -->
					</scroll-view>
				</swiper-item>
			</swiper>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				latitude: 39.909,
				longitude: 116.39742,
				covers: [
					{
						latitude: 39.9085,
						longitude: 116.39747,					
						iconPath: '/static/share_img/money.png'				
					}, 
					{
						latitude: 39.9085,
						longitude: 116.397,				
						iconPath: '/static/share_img/money.png'					
					},
					{
						latitude: 39.906,
						longitude: 116.396,				
						iconPath: '/static/share_img/money.png'					
					}
				],
				scrollLeft: 0,
				isClickChange: false,
				tabIndex: 0,
				loadingText:'加载更多...',
				recordList: [],
				tabBars: [{
					name: 'AD共享',
					id: 'gongxiang'
				}, {
					name: '返现记录',
					id: 'fanxian'
				}],
				total:'356.22',
				billList:[{
					"title": "点击广告返回红包",
					"date": "2019-05-31",
					"money": "+5"
				},{
					"title": "点击广告返回红包",
					"date": "2019-05-31",
					"money": "+0.5"
				},{
					"title": "点击广告返回红包",
					"date": "2019-05-30",
					"money": "+2.88"
				},{
					"title": "点击广告返回红包",
					"date": "2019-05-29",
					"money": "+8.88"
				},{
					"title": "点击广告返回红包",
					"date": "2019-05-28",
					"money": "+18.88"
				}]
				
			}
		},
		onLoad() {
			this.recordList = this.randomfn()
		},
		methods: {
			loadMore() {
				setTimeout(() => {
					this.addData();
				}, 1200);
			},
			addData() {
				if (this.recordList.length > 30) {
					this.recordList.loadingText = '没有更多了';
					return;
				}
				for (let i = 1; i <= 10; i++) {
					this.recordList.push(tpl['data' + Math.floor(Math.random() * 5)]);
				}
			},
			async changeTab(e) {
				let index = e.target.current;
				if (this.recordList.length === 0) {
					this.addData(index)
				}
				if (this.isClickChange) {
					this.tabIndex = index;
					this.isClickChange = false;
					return;
				}
				
				this.isClickChange = false;
				this.tabIndex = index; //一旦访问data就会出问题
				
			},
			getElSize(id) { //得到元素的size
				return new Promise((res, rej) => {
					uni.createSelectorQuery().select("#" + id).fields({
						size: true,
						scrollOffset: true
					}, (data) => {
						res(data);
					}).exec();
				})
			},
			async tapTab(e) { //点击tab-bar
				let tabIndex = e.currentTarget.dataset.current;
				if (this.recordList.length === 0) {
					this.addData(tabIndex)
				}
				if (this.tabIndex === tabIndex) {
					return false;
				} else {					
					this.isClickChange = true;
					this.tabIndex = tabIndex;
				}
			},
			randomfn() {
				let ary = [];
				for (let i = 0, length = this.tabBars.length; i < length; i++) {
					let aryItem = {
						loadingText: '加载更多...',
						data: []
					};
					if (i < 1) {
						for (let j = 1; j <= 10; j++) {
							aryItem.data.push("1");
						}
					}
					ary.push(aryItem);
				}
				return ary;
			}
		},
	}
</script>

<style lang="scss" scoped>
	@import '../../common/uni.css';	
	.container{
		padding-top:50upx;
		height:100vh;
		box-sizing: border-box;
		/*tab切换*/
		.uni-tab-bar{
			width:100%;
			height:100%;
			overflow: hidden;
			.uni-swiper-tab{
				margin-bottom:4upx;
				border-bottom:0;
				box-shadow: 4upx 4upx 16upx  rgba(71, 71, 71, 0.2);
				.swiper-tab-list{
					width:50%;
					text-align: right;
				}
				.swiper-tab-list view{
					display: inline-block;
					height:100upx;
					line-height: 100upx;
					margin:0 40upx;
					box-sizing: border-box;	
					font-size:32upx;
					color:#666666;
				}
				#fanxian{
					text-align: left;
				}
				.active view{
					color:#6167e7;
					border-bottom:6upx solid #6167e7;
				}
			}
		}		
	}
	/* 	加载更多 */
	.uni-tab-bar-loading {
		text-align: center!important;
		font-size: 28upx;
		color: #999;
	}
	
	/* 地图 */
	.map{
		width: 100%;
		height:65vh;
		margin-bottom:10upx;
	}
	map{
		width: 100%;
		height:100%;
		border:1px solid transparent;
	}
	/*广告图*/
	.article{
		width: 100%;
		height:288upx;
		position: absolute;
		top:0;
		left: 0;
		z-index: 999; 
		overflow: hidden;
		.cover-item{			
			width:65%!important;
			height:100%;
			background-repeat:no-repeat;
			background-size: 100% 100%;
			padding-left:40upx;
				.maxWord{
					color: #261dc0;
					font-size:36upx;
					font-weight:bold;
					margin-top:40upx;
				}
				.smallWord{
					color: #261dc0;
					font-size:28upx;
				}
				.yaoBtn{
					display: inline-block;
					line-height:2;
					color: #fff;
					font-size:28upx;
					margin-top:20upx;
					border-radius: 30upx;
					background: linear-gradient(to right, #3e9dff 5%,#5f5fe8 100%);
				}
				.seeBtn{
					background: linear-gradient(to right, #6c8bff 50%,#606fff 100%);
				}
			.image{
				width:168upx;
				height:200upx;
				padding-right:50upx;
			}
			
		}
	}
	/*返现记录*/
	.total{
		width:100%;
		height:210upx;
		text-align: center;
		view{
			margin-top:30upx;
			font-size:60upx;
			color:#393d51;
			font-weight:bold;
		}
		text{
			color:#8e92ac;
			margin-top:-10upx;
		}
	}
	.line{
		width:100%;
		height:16upx;
		background-color: #f0f0f7;
	}
	.box {
		width: 90%;
		margin:0 5%;
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 20upx 0;
		
		.left{
			display: flex;
			flex-direction: column;
			justify-content: center;
			
			.date {
				font-size: 24upx;
			}
		}
	}
	
</style>
